from PySide6.QtWidgets import QLabel
from PySide6.QtCore import Qt, QTimer


class ToastWidget(QLabel):
    """自动显示的提示气泡"""

    def __init__(self, parent, message, duration=1500):
        super().__init__(parent)
        self.setObjectName("toast")
        self.setText(message)
        self.setAlignment(Qt.AlignCenter)
        self.adjustSize()
        self._duration = duration
        self.show()

    def showEvent(self, event):
        super().showEvent(event)
        if self.parent():
            x = (self.parent().width() - self.width()) // 2
            y = self.parent().height() // 3
            self.move(x, y)
        QTimer.singleShot(self._duration, self.hide)


